package com.thinkgem.javamg.modules.busi.crawler;

import com.thinkgem.javamg.common.mapper.JsonMapper;
import com.thinkgem.javamg.common.utils.DateUtils;
import com.thinkgem.javamg.common.utils.SpringContextHolder;
import com.thinkgem.javamg.common.utils.StringUtils;
import com.thinkgem.javamg.modules.busi.dao.NewsDao;
import com.thinkgem.javamg.modules.busi.dto.ToutiaoDataDTO;
import com.thinkgem.javamg.modules.busi.dto.ToutiaoDetailDTO;
import com.thinkgem.javamg.modules.busi.dto.ToutiaoPageDTO;
import com.thinkgem.javamg.modules.busi.entity.News;
import com.thinkgem.javamg.modules.busi.service.NewsService;
import com.thinkgem.javamg.modules.busi.utils.HttpUtil;
import com.thinkgem.javamg.modules.busi.utils.ImageUtil;
import com.thinkgem.javamg.modules.busi.utils.JsoupUtil;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.List;

/**
 * 头条相关爬取
 */
public class CrawToutiao {

    protected static Logger logger = LoggerFactory.getLogger(CrawToutiao.class);
    private String[] userIds = new String[]{"50862059985", "4194310169", "62316539468", "5738835731", "52191280888", "6767732532", "3824873423", "6165687837", "6713177270", "6969296547", "4584493754", "6583982501", "50267015181", "5835169452", "58019011491", "5746721512", "4377744965", "59943159073", "51825214443", "5588514782", "5759061357", "6007491014", "52005820983", "54257656977", "54003606048", "5264845985", "59943922609"};
    private static NewsDao newsDao = SpringContextHolder.getBean(NewsDao.class);
    private static NewsService newsService = SpringContextHolder.getBean(NewsService.class);

    /**
     * 资讯列表
     *
     * @return
     */
    public void newsList() {
        for (String userId : userIds) {
            try {
                String jsonStr = HttpUtil.sendGet("https://www.toutiao.com/c/user/article/", "page_type=1&max_behot_time=0&count=20&user_id=" + userId);
                ToutiaoPageDTO page = (ToutiaoPageDTO) JsonMapper.fromJsonString(jsonStr, ToutiaoPageDTO.class);
                if (null != page && "success".equals(page.getMessage())) {
                    List<ToutiaoDataDTO> datas = page.getData();
                    for (ToutiaoDataDTO data : datas) {
                        //组装详情
                        News news = null;
                        try {
                            String title = data.getTitle();
                            if (newsDao.isexist(title) > 0 || data.isHas_video()) {
                                continue;
                            }

                            //下载列表图片
                            String picUrl = data.getImage_url();
                            if (StringUtils.isNotEmpty(picUrl) && !picUrl.contains("http:")) {
                                picUrl = "http:" + picUrl;
                            }
                            String imgPath = ImageUtil.downloadImageToDisk(picUrl);

                            news = newsDetail(data.getItem_id());
                            news.setTitle(title);
                            news.setSmallpicurl(imgPath);
                        } catch (Exception e) {
                            logger.error(e.getMessage());
                        }

                        //插入数据库
                        try {
                            newsService.save(news);
                        } catch (Exception e) {
                            logger.error(e.getMessage());
                        }

                        //等待
                        Thread.sleep(Constants.millis);
                    }
                }
            } catch (Exception e) {
                logger.error(e.getMessage());
            }
        }
    }

    /**
     * 图文详情
     *
     * @return
     */
    public News newsDetail(String item_id) {
        News news = new News();
        try {
            String jsonStr = HttpUtil.sendGet("https://m.toutiao.com/i" + item_id + "/info/", "");
            ToutiaoDetailDTO detailDTO = (ToutiaoDetailDTO) JsonMapper.fromJsonString(jsonStr, ToutiaoDetailDTO.class);
            if (null != detailDTO) {
                ToutiaoDataDTO data = detailDTO.getData();
                if (null != data) {
                    news.setContent(data.getContent());
                    news.setSource(data.getSource());
                    news.setAddtime(DateUtils.getDateTimeBySecond(data.getPublish_time()));
                    news.setTypeId(2);

                    if(StringUtils.isNotEmpty(news.getContent())){
                        Document doc=Jsoup.parse(news.getContent());
                        //下载详情第一张图为头图
                        String imgPath = ImageUtil.downloadImageToDisk(JsoupUtil.getFirstElementAttrText(doc.getElementsByTag("img"),"src"));
                        news.setPicurl(imgPath);
                    }

                    if(StringUtils.isEmpty(news.getSource())){
                        news.setSource("头条高尔夫");
                    }
                }
            }
        } catch (Exception e) {
            logger.error(e.getMessage());
        }
        return news;
    }
}
